package com.wlsk.license.common.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.wlsk.license.common.enums.BaseEnum;
import com.wlsk.license.common.exception.BusinessException;

import java.io.IOException;

/**
 * @description: 枚举序列化
 * @author: liuzhangbin
 * @date: 2025/2/20
 */
public class EnumSerializer extends JsonSerializer<Enum<?>> {

    @Override
    public void serialize(Enum<?> baseEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (baseEnum == null) {
            return;
        }
        if (baseEnum instanceof BaseEnum) {
            BaseEnum value = (BaseEnum) baseEnum;
            jsonGenerator.writeStartObject();
            jsonGenerator.writeObjectField("code", value.getCode());
            jsonGenerator.writeObjectField("description", value.getDescription());
            jsonGenerator.writeEndObject();
        } else {
            throw new BusinessException("不支持的枚举类型Json序列化");
        }
    }
}
